package com.vancouver.knows.ui.fragment.citydiscount;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.util.DeviceUtil;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.business.service.impl.BHttpService;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citydiscount.QrCodeActivity;
import com.vancouver.knows.ui.activity.citydiscount.QrCodeBusinessActivity;
import com.vancouver.knows.ui.activity.citydiscount.UseWelfareActivity;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.PromotionInfo;
import com.vancouver.knows.business.task.mark.UserDeleteFollowTaskMark;
import com.vancouver.knows.business.task.mark.UserFollowTaskMark;

public class PromotionsView implements IResultReceiver {

	private static final String TAG = PromotionsView.class.getSimpleName();

	private CityDiscountDetailInfo cityDiscountDetailInfo;
	private LinearLayout rootView;
	private CityDiscountDetailActivity activity;

	public PromotionsView(final CityDiscountDetailActivity activity){
		this.activity = activity;
	}

	public void init(final String qrCodeBitmapString, LinearLayout rootView,
					 final CityDiscountDetailInfo cityDiscountDetailInfo) {

		this.cityDiscountDetailInfo = cityDiscountDetailInfo;
		this.rootView = rootView;
		List<PromotionInfo> promotions = cityDiscountDetailInfo.getPromotions();
		rootView.removeAllViews();
		if (promotions == null || promotions.size() == 0) {
			View view = LayoutInflater.from(activity).inflate(R.layout.view_promotion_item, null);
			TextView title = (TextView) view.findViewById(R.id.title);
			title.setText(R.string.no_welfare);
			TextView message = (TextView) view.findViewById(R.id.message);
			TextView promotionBtn = (TextView) view.findViewById(R.id.promotionBtn);
			if ("Y".equals(cityDiscountDetailInfo.getUserFollowed())) {
				message.setText(R.string.followed_welfare);
				promotionBtn.setText(R.string.welfare_followed);
				promotionBtn.setBackgroundResource(R.drawable.promotion_type_grey_yellow_bg);
			} else {
				message.setText(R.string.follow_welfare);
				promotionBtn.setText(R.string.welfare_follow);
				promotionBtn.setBackgroundResource(R.drawable.promotion_type_orange_bg);
			}

			promotionBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (!AppUtils.getLoginStatus(activity)) {
						return;
					}

					BusinessModule bModule = ((MainApplication) activity.getApplication()).getBusinessModule();
					String userId = bModule.getCacheManager().getUserInfo() == null ? "" : bModule.getCacheManager()
							.getUserInfo().getUserID();
					if ("Y".equals(cityDiscountDetailInfo.getUserFollowed())) {
						UserDeleteFollowTaskMark aTaskMark = bModule.getTaskMarkPool().getUserDeleteFollowTaskMark(
								cityDiscountDetailInfo.getId());
						bModule.getServiceWrapper().userDeleteFollow(PromotionsView.this, aTaskMark,
								cityDiscountDetailInfo.getId(), userId);

					} else {
						UserFollowTaskMark aTaskMark = bModule.getTaskMarkPool().getUserFollowTaskMark(
								cityDiscountDetailInfo.getId());
						bModule.getServiceWrapper().userFollow(PromotionsView.this, aTaskMark,
								cityDiscountDetailInfo.getId(), userId);

						Tracker t = ((MainApplication) activity.getApplication()).getTracker(TrackerName.APP_TRACKER);
						t.setScreenName(AnalyticsConstants.CUSTOM_EVENT_VALUE_CITYDISCOUNT_DETAIL);
						t.send(new HitBuilders.EventBuilder().setCategory(AnalyticsConstants.CATEGORY_BTN)
								.setAction(AnalyticsConstants.ACTION_ONCLICK)
								.setLabel(AnalyticsConstants.LABEL_CITYDISCOUNT_DETAIL_FOLLOW).build());

					}
				}
			});
			rootView.addView(view);

		} else {

			int count = 0;
			for (int i = 0; i < promotions.size(); i++) {
				final PromotionInfo promotionInfo = promotions.get(i);
				View view = LayoutInflater.from(activity).inflate(R.layout.view_promotion_discount_item, null);
				TextView title = (TextView) view.findViewById(R.id.title_text);
				title.setText(promotionInfo.getTitle());
				TextView message = (TextView) view.findViewById(R.id.message_text);
				message.setText(promotionInfo.getMessage());
				View flDivider = (View) view.findViewById(R.id.fl_divider);
				flDivider.setVisibility(View.GONE);
				TextView flQrCode = (TextView) view.findViewById(R.id.fl_qr_code);
				flQrCode.setVisibility(View.GONE);
				count++;
				if (promotions.size() > i && count != promotions.size()) {
					flDivider.setVisibility(View.VISIBLE);
				}
				if (count == promotions.size()) {
					flQrCode.setVisibility(View.VISIBLE);
					flQrCode.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View view) {
							Intent intent = new Intent(activity, QrCodeBusinessActivity.class);
							intent.putExtra("QR_CODE_BITMAP", qrCodeBitmapString);
							activity.startActivity(intent);
						}
					});
				}

//				ImageView qrImage = (ImageView) view.findViewById(R.id.qr_code);
//				createQrImage(promotionInfo.getCode(), qrImage);
//
//				TextView name = (TextView) view.findViewById(R.id.name);
//				name.setText(cityDiscountDetailInfo.getName());
//				TextView promotionId = (TextView) view.findViewById(R.id.promotion_id);
//				promotionId.setText(promotionInfo.getCode());
//				TextView welfareTitle = (TextView) view.findViewById(R.id.welfare_text);
//				welfareTitle.setText(promotionInfo.getTitle());
//
//				TextView instructions = (TextView) view.findViewById(R.id.instructions);
//				instructions.setText(promotionInfo.getRedeemInstructions());

//				final LinearLayout promoLayout = (LinearLayout) view.findViewById(R.id.promo_info);

//				final TextView promoExpandButton = (TextView) view.findViewById(R.id.description_expand_button);
//				promoExpandButton.setOnClickListener(new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						if (promoLayout.getVisibility() == View.GONE) {
//							promoLayout.setVisibility(View.VISIBLE);
//							Drawable drawable = ContextCompat.getDrawable(activity, R.drawable.icon_arrow_up);
//							drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//							promoExpandButton.setCompoundDrawables(null, null, drawable, null);
//							promoExpandButton.setCompoundDrawablePadding(AppUtils.dip2px(activity, 8)) ;
//						} else {
//							promoLayout.setVisibility(View.GONE);
//							Drawable drawable = ContextCompat.getDrawable(activity, R.drawable.icon_arrow_down);
//							drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//							promoExpandButton.setCompoundDrawables(null, null, drawable, null);
//							promoExpandButton.setCompoundDrawablePadding(AppUtils.dip2px(activity, 8)) ;
//						}
//					}
//				});

				rootView.addView(view);
			}
		}
	}

	public void updateLayout() {
		if (cityDiscountDetailInfo.getPromotions().size() > 0) {
			return;
		}

		View view = rootView.getChildAt(0);
		TextView title = (TextView) view.findViewById(R.id.title);
		title.setText(R.string.no_welfare);
		TextView message = (TextView) view.findViewById(R.id.message);
		TextView promotionBtn = (TextView) view.findViewById(R.id.promotionBtn);
		if ("Y".equals(cityDiscountDetailInfo.getUserFollowed())) {
			message.setText(R.string.followed_welfare);
			promotionBtn.setText(R.string.welfare_followed);
			promotionBtn.setBackgroundResource(R.drawable.promotion_type_grey_yellow_bg);
		} else {
			message.setText(R.string.follow_welfare);
			promotionBtn.setText(R.string.welfare_follow);
			promotionBtn.setBackgroundResource(R.drawable.promotion_type_yellow_bg);
		}
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		if ((taskMark instanceof UserFollowTaskMark || taskMark instanceof UserDeleteFollowTaskMark)
				&& taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			updateLayout();
			activity.updateMenu();
		}
	}
}
